package nl.han.ica.ap.searchengine.shared.sdo;

/**
  * Simple data object that represents a webpage. It includes properties
  * for the url, the HTML contents of the webpage and the PageRank value
  * of the webpage.
  * 
  * <p>Webpage objects are not guaranteed to have a value for each of the
  * mentioned properties. Depending on the constructor used some properties
  * will have null values.</p>
  * 
  * <p><strong>Usage:</strong></p>
  * 
  * <p>If this object is to be created just after gathering the information
  * of a webpage, it is advised to use the {@link Webpage(String, String)}
  * constructor, as the PageRank property can only be calculated after all
  * webpages have been indexed and have a corresponding Webpage object.</p>
  * 
  * <p>In the event that this class is used to store a webpage's content in
  * a database, it is advised to use the {@link Webpage(String, String, Integer)}
  * constructor. This ensures that all required data can and will be stored
  * in the database.</p>
  * 
  * <p>When using the Webpage object to display search results it is advised
  * to use the {@link Webpage(String, Integer)} constructor. This way memory
  * is saved by not storing the HTML contents of the webpage as this is not
  * required for displaying search results.</p>
  * 
  * @author W. ter Berg
  */
public class Webpage {

	private String url;
	private String contents;
	private Integer rank;

	private Webpage(final String url) {
		this.url = url;
	}
	
	/**
	  * Creates a new Webpage object with the given arguments stored
	  * as properties.
	  * 
	  * @param url The URL of the webpage.
	  * @param contents The HTML contents of the webpage.
	  */
	public Webpage(final String url, final String contents) {
		this(url);
		this.contents = contents;
	}

	/**
	  * Creates a new Webpage object with the given arguments stored
	  * as properties.
	  * 
	  * @param url The URL of the webpage.
	  * @param rank The PageRank of the webpage.
	  */
	public Webpage(final String url, final Integer rank) {
		this(url);
		this.setRank(rank);
	}

	/**
	  * Creates a new Webpage object with the given arguments stored
	  * as properties.
	  * 
	  * @param url The URL of the webpage.
	  * @param contents The HTML contents of the webpage.
	  * @param rank The PageRank of the webpage.
	  */
	public Webpage(final String url, final String contents, final Integer rank) {
		this(url, contents);
		this.setRank(rank);
	}

	/**
	  * Returns the URL property of this object.
	  * 
	  * @return The URL of this webpage.
	  */
	public String getUrl() {
		return this.url;
	}

	/**
	  * Returns the HTML property of this object.
	  * 
	  * @return The HTML contents of this webpage.
	  */
	public String getContents() {
		if (this.contents == null)
			throw new UnsupportedOperationException("Cannot return the contents property when this property was not set.");
		
		return this.contents;
	}

	/**
	  * Returns the PageRank property of this object.
	  * 
	  * @return The PageRank of this webpage.
	  */
	public Integer getRank() {
		if (this.rank == null)
			throw new UnsupportedOperationException("Cannot return the PageRank property when this property was not set.");
		
		return this.rank;
	}

	/**
	  * Setter method for the PageRank property. The new value cannot be
	  * a negative number.
	  * 
	  * @param newRank The new PageRank value to store.
	  */
	public void setRank(final Integer newRank) {
		if (newRank < 0)
			throw new IllegalArgumentException("PageRank attribute cannot be set to a value below 0. " + newRank + " given.");
		
		this.rank = newRank;
	}

}
